﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controllers;

namespace UpgradeTool
{
    public partial class CompareControl : UserControl
    {
        public CompareControl()
        {
            InitializeComponent();
        }

        private void leftBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!leftBrowser.IsBusy && leftBrowser.Url == e.Url && leftBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                leftBrowser.Document.Window.Scroll += leftBrowserScrollHandler;
            }
        }

        private void centralBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!centralBrowser.IsBusy && centralBrowser.Url == e.Url && centralBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                centralBrowser.Document.Window.Scroll += centralBrowserScrollHandler;
            }
        }

        private void rightBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!rightBrowser.IsBusy && rightBrowser.Url == e.Url && rightBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                rightBrowser.Document.Window.Scroll += rightBrowserScrollHandler;
            }
        }

        private void leftBrowserScrollHandler(object sender, EventArgs e)
        {
            if ((ActiveControl != null) && (ActiveControl.Name == "leftBrowser"))
            {
                var scrolledBrowser = sender as HtmlWindow;
                if (scrolledBrowser == null) return;
                centralBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
                rightBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
            }
        }

        private void centralBrowserScrollHandler(object sender, EventArgs e)
        {
            if ((ActiveControl != null) &&  (ActiveControl.Name == "centralBrowser"))
            {
                var scrolledBrowser = sender as HtmlWindow;
                if (scrolledBrowser == null) return;
                leftBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
                rightBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
            }
        }

        private void rightBrowserScrollHandler(object sender, EventArgs e)
        {
            if ((ActiveControl != null) && (ActiveControl.Name == "rightBrowser"))
            {
                var scrolledBrowser = sender as HtmlWindow;
                if (scrolledBrowser == null) return;
                centralBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
                leftBrowser.Document.Window.ScrollTo(0, scrolledBrowser.Document.GetElementsByTagName("HTML")[0].ScrollTop);
            }
        }

        public void CompareScrolling(int value)
        {
            rightBrowser.Document.Window.ScrollTo(0, value);
            centralBrowser.Document.Window.ScrollTo(0, value);
            leftBrowser.Document.Window.ScrollTo(0, value);
        }

        private void PreviusSelectButton_Click(object sender, EventArgs e)
        {
            InsertFileNames(PreviusSelectPath);
        }

        private void CustomerSelectButton_Click(object sender, EventArgs e)
        {
            InsertFileNames(CustomerSelectPath);
        }

        private void NextSelectButton_Click(object sender, EventArgs e)
        {
            InsertFileNames(NextSelectPath);
        }

        private void InsertFileNames(TextBox textbox)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textbox.Text = openFileDialog1.FileName;
            }
        }

    }
}
